<template>

  <div class="register">
    <el-container>
      <el-header class="header">

        <div class="re-top">
          <img src="../assets//images/logo38.png">
          <p class="p1">能力开放平台</p>
          <p class="p2">我已注册，现在就</p>
          <el-button type="primary" @click="gologin">登录</el-button>
        </div>
        <div class="foot-bj"></div>

      </el-header>
      <el-main>
        <div class="down">
          <el-form ref="registerForm" :model="registerForm" :rules="registerRules" class="register-form">
            <div class="title-top">
              <div class="title-style"></div>
              <h3 class="title">欢迎加入我们</h3>
            </div>
            <!-- 用户名 -->
            <el-form-item prop="username" label-width="180px" label="用户名:">

              <el-input v-model="registerForm.username" type="text" auto-complete="off"
                placeholder="5-15位,必须包含字母、数字和特殊字符,如:dc-^33名">
                <!-- <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" /> -->
              </el-input>
            </el-form-item>
            <!-- 密码 -->
            <el-form-item prop="password" label-width="180px" label="密码:">
              <el-input v-model="registerForm.password" type="password" auto-complete="off"
                placeholder="8-15位,必须包含字母、数字和特殊字符,如:dc-^33" @keyup.enter.native="handleRegister">
                <!-- <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" /> -->
              </el-input>
            </el-form-item>
            <!-- 确认密码 -->
            <el-form-item prop="confirmPassword" label-width="180px" label="确认密码:">
              <el-input v-model="registerForm.confirmPassword" type="password" auto-complete="off" placeholder="确认密码"
                @keyup.enter.native="handleRegister">
                <!-- <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" /> -->
              </el-input>
            </el-form-item>
            <!-- 手机号 -->
            <el-form-item prop="phone" label-width="180px" label="手机号:">
              <div style="display:flex">
                <!-- 下拉选择框 -->
                <el-select v-model="phone" disabled>
                  <el-option selected>+86(中国)</el-option>
                </el-select>
                <el-input v-model="registerForm.phone" type="text" auto-complete="off" placeholder="请输入您的手机号">
                  <!-- <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" /> -->
                </el-input>
              </div>
            </el-form-item>
            <!-- 验证码 -->
            <el-form-item prop="code" label-width="180px" label="验证码:">
              <div style="display: flex;width:470px">
                <el-input style="width: 180px" v-model="registerForm.code" type="text" auto-complete="off">
                  <!-- <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" /> -->
                </el-input>
                <button class="verification">
                  免费获取短信验证码
                </button>
              </div>
            </el-form-item>

            <el-form-item prop="birthday" label-width="180px" label="出生日期:">
              <div id="myDate">
                <el-select v-model="yearsModel"  @change="dateChange(1)" placeholder="请选择" style="width:100px;margin-right:20px">
                  <el-option v-for="item in years" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
                <el-select v-model="monthsModel" @change="dateChange(2)" placeholder="请选择" style="width:100px;margin-right:20px">
                  <el-option v-for="item in months" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
                <el-select v-model="daysModel" @change="dateChange(3)" placeholder="请选择" style="width:100px;margin-right:20px">
                  <el-option v-for="item in days" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </div>
            </el-form-item>

            <el-form-item prop="sex" label-width="180px" label="性别:">
              <div class="radio">
                <el-radio v-model="registerForm.sex" label="1">男</el-radio>
                <el-radio v-model="registerForm.sex" label="2">女</el-radio>
              </div>
            </el-form-item>



            <!-- <el-form-item prop="code" v-if="captchaEnabled">
              <el-input v-model="registerForm.code" auto-complete="off" placeholder="验证码" style="width: 63%"
                @keyup.enter.native="handleRegister">
                <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
              </el-input>
              <div class="register-code">
                <img :src="codeUrl" @click="getCode" class="register-code-img" />
              </div>
            </el-form-item> -->
            <el-form-item style="width:100%;">
              <el-button :loading="loading" size="medium" type="primary" style="width:100%;"
                @click.native.prevent="handleRegister">
                <span v-if="!loading">注 册</span>
                <span v-else>注 册 中...</span>
              </el-button>
              <!-- <div style="float: right;">
                <router-link class="link-type" :to="'/login'">使用已有账户登录</router-link>
              </div> -->
            </el-form-item>
          </el-form>
        </div>
      </el-main>
    </el-container>





    <!--  底部  -->
    <div class="el-register-footer">
      <span>Copyright © 2018-2023 ruoyi.vip All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg, register } from "@/api/login";

export default {
  name: "Register",
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.registerForm.password !== value) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    return {
      codeUrl: "",
      registerForm: {
        username: "",
        password: "",
        confirmPassword: "",
        monthsModel:"",
        daysModel:"",
        phone: "",
        code: "",
        yearsModel:"",
        uuid: "",
        sex: "",
      },
      yearsModel:null,
        years:[],
        monthsModel:null,
        months:[],
        daysModel:null,
        days:[],

      radio: '1',

      registerRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" },
          { min: 5, max: 15, message: '用户账号长度必须介于 5 和 15 之间', trigger: 'blur' },
          {
            pattern: /(?![A-Z]*$)(?![a-z]*$)(?![0-9]*$)(?![^a-zA-Z0-9]*$)/,
            message: '必须包含字母、数字和特殊字符',
            trigger: 'blur'
          },
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
          { min: 5, max: 20, message: "用户密码长度必须介于 5 和 20 之间", trigger: "blur" },
          { pattern: /(?![A-Z]*$)(?![a-z]*$)(?![0-9]*$)(?![^a-zA-Z0-9]*$)/, message: "不能包含非法字符：< > \" ' \\\ |", trigger: "blur" }
        ],
        confirmPassword: [
          { required: true, trigger: "blur", message: "请再次输入您的密码" },
          { required: true, validator: equalToPassword, trigger: "blur" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }],
        phone: [{ required: true, trigger: "blur", message: "请输入手机号" }],
        // birthday: [{ required: true, trigger: "change", message: "请选择出生日期" }],
        sex: [{ required: true, trigger: "change", message: "请选择性别" }]

      },
      loading: false,
      captchaEnabled: true
    };
  },

  created() {
    this.getCode();
    this.init();
  },
  methods: {
    // 验证码
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.registerForm.uuid = res.uuid;
        }
      });
    },
    init(){
          var myDate = new Date;
          var year = myDate.getFullYear();//获取当前年
          var month = myDate.getMonth()+1;//获取当前月
          var day = myDate.getDate();//获取当前日
 
          this.initSelectYear(year)
          this.initSelectMonth();
          this.initSelectDay(year,month);
          this.yearsModel = year;
          this.monthsModel = month;
          this.daysModel = day;
          let obj ={year:this.yearsModel,month:this.monthsModel,day:this.daysModel }
          this.$parent.dateChange(obj);
      },
      initSelectYear(year){
           this.years = [];
           for(let i=0;i<30;i++){
            this.years.push({value:(year - i),label:(year - i)+ "年"});
           }
      },
      initSelectMonth(){
          this.months = [];
          this.months.push({value:0,label:"全部"});
           for(let i=1;i<=12;i++){
            this.months.push({value:i,label:i+ "月"});
           }
      },
       initSelectDay(year,month){
          var maxDay = this.getMaxDay(year,month);
          this.days = [];
          this.days.push({value:0,label:"全部"});
          for(var i=1;i<=maxDay;i++){
              this.days.push({value:i,label:i+ "日"});
          }
      },
      dateChange(type){
        //1年 2月 3日 4 左 5右
        if(type == 1||type == 2){
            if(this.monthsModel == 0){
              this.daysModel = 0;
              this.initSelectDay(this.yearsModel,1);
            }else{
              this.initSelectDay(this.yearsModel,this.monthsModel);
            }
        }
        if(type == 4){
          this.dayleft();
        }
        if(type == 5){
          this.dayright();
        }
 
        //操作父组件方法
        let obj ={year:this.yearsModel,month:this.monthsModel,day:this.daysModel }
        this.$parent.dateChange(obj);
        
      },
      getMaxDay(year,month){
            var new_year = year;  //取当前的年份
            var new_month = month++;//取下一个月的第一天，方便计算（最后一天不固定）
            if(month>12)      //如果当前大于12月，则年份转到下一年
            {
                new_month -=12;    //月份减
                new_year++;      //年份增
            }
            var new_date = new Date(new_year,new_month,1);//取当年当月中的第一天
            return (new Date(new_date.getTime()-1000*60*60*24)).getDate();//获取当月最后一天日期
     },
    
    gologin() {
      this.$router.push("/login")
    },

    handleRegister() {
      // 注册按钮
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          this.loading = true;
          register(this.registerForm).then(res => {
            const username = this.registerForm.username;
            this.$alert("<font color='red'>恭喜你，您的账号 " + username + " 注册成功！</font>", '系统提示', {
              dangerouslyUseHTMLString: true,
              type: 'success'
            }).then(() => {
              this.$router.push("/login");
            }).catch(() => { });
          }).catch(() => {
            this.loading = false;
            if (this.captchaEnabled) {
              this.getCode();
            }
          })
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.register {
  position: relative;
  height: 100%;
  background-color: white;
  background-size: cover;
}

.title-top {
  margin: 100px 0;
  height: 50px;
  display: flex;
  align-items: center;
  text-align: center;
}

.title-style {
  height: 30px;
  width: 5px;
  text-align: center;
  border-radius: 5px;

  background-color: rgb(7, 152, 254);

}


.title {
  margin-left: 5px;
  // margin: 0px auto 100px auto;
  text-align: center;
  color: #707070;
  height: 50px;
  font-size: 30px;
  line-height: 50px;
}

.header {
  padding: 0;
  position: absolute;
  height: 75px;
  background-color: white;
  width: 100%;

  .re-top {
    height: 60px;
    display: flex;
    align-items: center;
    padding-left: 150px;
    width: 100%;

    img {
      height: 40px;
      width: 40px;
      float: left;
    }

    .p1 {
      width: 200px;
      min-width: 200px;
    }

    .p2 {
      margin-left: 1200px;
      margin-right: 10px;
    }

  }
}

.el-form-item__label {
  width: 180px;
  text-align: left;

}

.radio {
  display: flex;
  width: 200px;
  padding: 10px;
}

.foot-bj {
  background-image: url("../assets/images/foot_bj2.png");
  height: 15px;
}

.down {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
}

.register-form {
  border-radius: 6px;
  background: #ffffff;
  width: 650px;
  padding: 25px 25px 5px 25px;
  text-align: left;

  .el-input {
    height: 38px;

    input {
      height: 38px;
    }
  }

  .verification {
    width: 220px;
    margin-left: 20px;
    box-sizing: border-box;
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.register-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.register-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-register-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.register-code-img {
  height: 38px;
}
</style>
